# class Student(object):
#     def __init__(self, name):
#         self.__name = name
#
#     def set_name(self, name):
#         self.__name = name
#
#     def get_name(self, object1):
#         print(self.__name)
#         object1.run();
#
#
# class Testw(object):
#     def run(self):
#         print("动态语言特性")
#
#
# studet = Student("lizi")
# studet.__name = "张三"
#
# studet.get_name(Testw())
# print(studet.__name)
#
# print(' ')
# print('=====================dict字典{}====================================')
# d = {'a':'111','b':333,'e':'eeeee','f':'fffff'}
# print(d.get('a'))
# print(d['a'])
# c = d.copy()
# d['a'] = 4444
# c.pop('b')
# print(c)
# print(d)
#
# print(' ')
# print('=====================set([])====================================')
# seta = set(['aa','aa','ccc'])
# seta.add("bbb")
# seta.add("eeeee")
# seta.add("ffffff")
# print(seta)
# seta.remove("aa")
# seta.pop()
# seta.pop()
# print(seta)

# print(' ')
# print('=======================list[]==================================')
# lista = ['1111','2222','3333',44444]
# listb = ['999','8888','7777']
# lista.extend(listb)
# print(lista)
# lista.pop()
# lista.append('aaaa')
# print(lista)
#
# print(' ')
# print('=========================tuple()================================')
# tuplea = (1111,222,333,222)
# print(tuplea)
# print(tuplea[2])
# print(tuplea.index(222))

# print(' ')
# print('=========================函数================================')
#
# def func1(x,y,z=4):
#     print('x:%s'%x,'y:%s'%y ,'z:%s'%z)
#
# func1(1,2)
# func1(z=9,y=8,x=7)
# func1(*([99,88,77]))
#
# def func2( *args):
#     for a in args:
#         print(a)
#
# func2(2,3,4)
# func2(*(22,33))
#
# def func3( **args):
#     print(args)
#
# func3(**{"a":111,"b":2222})
# func3(a=3,b=4)
#
# def func4(*,ab):
#     print(ab)
#
# func4(ab = 3)
#
#
# def func5(a,b,*c,**d):
#     print(a)
#     print(b)
#     print(c)
#     print(d)
#
#
# func5(1,2,3,4,**{"aa":11,"bb":222})


#
# print(' ')
# print('=========================高级特性================================')
#
# list = [11,22,33,44,55,66,77,88,99]
# newlsit = list[1:8:2]
# newlsit = list[::-1]
# print(newlsit)
# print(list)
#

#
# print(' ')
# print('=========================响应式编程================================')
# def func6(x):
#     return x*x
# print("map:",list(map(func6,[1,2,3,4])))  #list
# print("map:",list(map(func6,(1,2,3,4))))  #tuple
# print("map:",list(map(func6,([1,2,3,4])))) #set
#
#
#
# def func7(x):
#     return x>5
# print("filter",list(filter(func7,(3,4,5,6,7,8,9))))
#
# print (sorted([5,3,7,1,5,6]))
#
#
# def func8(a):
#     def funcn1(b):
#         print(a,"=====",b)
#     return funcn1
#
# funcval = func8(3)
# funcval(5)
# func8(4)(8)
#
#
# def func9a(func):
#     # def wrapper(a,b):
#     #     print(func.__name__,'函数增强')
#     #     return func(a+a,b+b)
#     # return wrapper
#
#     print(func.__name__,'函数增强')
#     return func
#
#
# @func9a
# def func9b(a,b):
#     print(a,"----",b)
#
# func9b(1,2)
# import asyncio
import sys

# from tkinter import Label, Button, Frame
#
# import requests
import time

print(sys.version)

# print(sys.version)
# # r = requests.get('https://www.douban.com/')
# r = requests.post('https://accounts.douban.com/login',
#                   data={'form_email': 'abc@example.com', 'form_password': '123456'})
# print(r.text)


# class Application(Frame):
#     def __init__(self, master=None):
#         Frame.__init__(self, master)
#         self.pack()
#         self.createWidgets()
#
#     def createWidgets(self):
#         self.helloLabel = Label(self, text=r.text)
#         self.helloLabel.pack()
#         self.quitButton = Button(self, text='Quit', command=self.quit)
#         self.quitButton.pack()
#
# app = Application()
# # 设置窗口标题:
# app.master.title('html')
# # 主消息循环:
# app.mainloop()

#
#
# def fun1():
#     print("fun1 init")
#     r = yield
#     print(r)
#     yield "fun1传递的数据"
#
# def fun2(f):
#     print("fun2 init")
#     f.send(None)
#     r = f.send( "fun2传递的数据")
#     print(r)$ pip install flask
#     f.close()
#
# fun2(fun1())


# @asyncio.coroutine
# def fun3():
#     print('Hello world! ' )
#     yield from asyncio.sleep(1)
#     print('Hello again! ' )
#
# loop = asyncio.get_event_loop()
# tasks = [fun3(), fun3()]
# loop.run_until_complete(asyncio.wait(tasks))
# loop.close()
# #
#
# r = lambda x:1+x
# print(r(1))
#
# @asyncio.coroutine
# def fun3():
#     print("111111111")
#     f = lambda :"qqqqqq"
#     r =  yield from  f()
#     print("222222222",r)
#
# loop = asyncio.get_event_loop()
# tasks = [fun3(), fun3()]
# loop.run_until_complete(asyncio.wait(tasks))
# loop.close()


import threading
import asyncio


# @asyncio.coroutine
# def hello2():
#     print('Hello 22222222! (%s)' % threading.currentThread())
#     return "aa"
#
# @asyncio.coroutine
# def hello():
#     print('Hello world! (%s)' % threading.currentThread())
#     aa = yield from hello2()
#     print('Hello again! (%s)' % threading.currentThread())
#
# loop = asyncio.get_event_loop()
# tasks = [hello(), hello()]
# loop.run_until_complete(asyncio.wait(tasks))
# loop.close()

#
# @asyncio.coroutine
# def test1():
#     print("test1111")
#     yield from asyncio.sleep(2)
#     print("test12222")
#     yield from asyncio.sleep(2)
#
#
# @asyncio.coroutine
# def test2():
#     print("test2AAAAAAA")
#     yield from test1()
#     print("tes2BBBBBBB")
#
#
# loop = asyncio.get_event_loop()
# tasks = [test2(), test2()]
# loop.run_until_complete(asyncio.wait(tasks))
# loop.close()


# @asyncio.coroutine
# def wget(host):
#     print('wget %s...' % host)
#     yield from test3()
#
#     connect = asyncio.open_connection(host, 80)
#     reader, writer = yield from connect
#     header = 'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % host
#     writer.write(header.encode('utf-8'))
#
#     yield from writer.drain()
#     while True:
#         line = yield from reader.readline()
#         if line == b'\r\n':
#             break
#         print('%s header > %s' % (host, line.decode('utf-8').rstrip()))
#     # Ignore the body, close the socket
#     writer.close()
#
#
# loop = asyncio.get_event_loop()
# tasks = [wget(host) for host in ['www.sina.com.cn', 'www.sohu.com', 'www.163.com']]
# loop.run_until_complete(asyncio.wait(tasks))
# loop.close()

import asyncio

@asyncio.coroutine
def wget(host):
    print('wget %s...' % host)
    connect = asyncio.open_connection(host, 80)
    reader, writer = yield from connect
    print('11111...' )
    header = 'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % host
    writer.write(header.encode('utf-8'))
    yield from writer.drain()
    print('2222222...' )

    while True:
        line = yield from reader.readline()
        if line == b'\r\n':
            break
        print('%s header > %s' % (host, line.decode('utf-8').rstrip()))
    # Ignore the body, close the socket
    writer.close()

loop = asyncio.get_event_loop()
tasks = [wget(host) for host in ['www.sina.com.cn', 'www.sohu.com', 'www.163.com']]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
